# -*- coding: utf-8 -*-
"""
Created on Sat Apr 22 00:48:24 2023

@author: 52224
"""
#=****载入相关仪器功能路径****=#
import sys
sys.path.append('D:\\Seafile\\Yijun_Li\\Auto_Test_Project\\B1500\\') #B1500.py存储的位置
#=****载入相应函数****=#
from B1500 import IV_sweept
# import matplotlib.pyplot as plt
#=****载入B1500****=#
# import pyvisa
# rm=pyvisa.ResourceManager()
# B1500=rm.open_resource('GPIB0::18::INSTR')
# B1500.timeout=60000
# B1500.write('*RST')
# B1500.write('CN 1,2,3,4,5,6')
#=****定义IDVG函数测试常量****=#
## -> VG端口的相关设置
# VG_Unit='2'
# Start_VG=5.0
# End_VG=-5.0
# nop_VG=201
# VG_com=10e-6
# Range_VG='Limited 1nA'
# VG_setup=[VG_Unit,Start_VG,End_VG,nop_VG]

## -> VS端口的相关设置
# VS_Unit='3'
# VS_comp=1e-2
# VS_Voltage=0.0
# Range_VS='Limited 1nA'
# VS_setup=[VS_Unit,VS_comp]

## -> VD端口的相关设置
# VD_Voltage=-1.0
# VD_comp=1e-2
# VD_Unit1='1'
# VD_Unit2='4'
# VD_Unit3='5'
# VD_Unit4='6'
# VD_setup=[VD_Voltage,VD_comp,VD_Unit1,VD_Unit2,VD_Unit3,VD_Unit4]

## -> 运行和画图
# data=Transistor_Test_IDVD(VD_VG_setup,VS_Unit)
# data1=np.float32(data[1:,:])
# plt.plot(data1[:,0],data1[:,1])
def Transistor_Test_IDVG(B1500,VG_setup,VS_setup,VD_setup,VG_comp=10e-6,VS_Voltage=0.0,
                         Range_VG='Limited 1nA',Range_VS='Limited 1nA',Range_VD='Limited 1nA'):
    Unit1=VG_setup[0]
    VName1='VG'
    Direction1='Double Linear'
    Start1=VG_setup[1]
    end1=VG_setup[2]
    nop1=VG_setup[3]
    Icomp1=VG_comp
    range1=Range_VG
    VAR_setup=[Unit1,VName1,Direction1,Start1,end1,nop1,Icomp1,range1]
    
    Unit2=VS_setup[0]
    VName2='VS'
    Voltage2=VS_Voltage
    Icomp2=VS_setup[1]
    Range2=Range_VS
    Measurement_setup=[Unit2,VName2,Voltage2,Icomp2,Range2]
    i=0
    while(1):
        try:
            locals()['Unit'+str(i+3)]=VD_setup[i+2]
            locals()['VName'+str(i+3)]='VD'+str(i+1)
            Measurement_setup+=[locals()['Unit'+str(i+3)],locals()['VName'+str(i+3)]
                                ,VD_setup[0],VD_setup[1],Range_VD]
            i+=1
        except:
            break
    
    return IV_sweept(B1500,VAR_setup,Measurement_setup,t_hold=0.0,
                   s_delay=0.0,t_delay=0.0)

#=****定义IDVD函数测试常量****=#
## -> VD的扫描参数设置
# VD_Unit='1'
# VD_Start=0.0
# VD_end=-3.0
# VD_nop=201
# ID_comp=1e-2

## -> VG的扫描参数设置
# VG_Unit='2'
# VG_Start=-5.0
# VG_step2=0.5
# VG_nop=7
# VD_VG_setup=[VD_Unit,VD_Start,VD_end,VD_nop,ID_comp,
#              VG_Unit,VG_Start,VG_step2,VG_nop]

## -> VS的端口设置
# VS_Unit='3'
# 默认VS电压为0，且同时只测一个器件，因为判断IDVD测试量不大，如果需要像IDVG一样并行可以简单改一下

def Transistor_Test_IDVD(VD_VG_setup,VS_Unit,Comp_IG=10e-6,VS_voltage=0.0,Comp_IS=1e-2,
                         Range_VD='Limited 1nA',Range_VG='Limited 1nA',
                         Range_VS='Limited 1nA'):
    Unit1=VD_VG_setup[0]
    VName1='VD'
    Direction1='Double Linear'
    Start1=VD_VG_setup[1]
    end1=VD_VG_setup[2]
    nop1=VD_VG_setup[3]
    Icomp1=VD_VG_setup[4]
    range1=Range_VD

    Unit2=VD_VG_setup[5]
    VName2='VG'
    Start2=VD_VG_setup[6]
    step2=VD_VG_setup[7]
    nop2=VD_VG_setup[8]
    Icomp2=Comp_IG
    range2=Range_VG
    VAR_setup=[Unit1,VName1,Direction1,Start1,end1,nop1,Icomp1,
            range1,Unit2,VName2,Start2,step2,nop2,Icomp2,range2]
    Unit3=VS_Unit
    VName3='VS'
    Voltage3=VS_voltage
    Icomp3=Comp_IS
    range3=Range_VS
    Measurement_setup=[Unit3,VName3,Voltage3,Icomp3,range3]
    return IV_sweept(B1500,VAR_setup,Measurement_setup,t_hold=0.0,
               s_delay=0.0,t_delay=0.0)